// subpackage/pages/complaint/add.js
import requestUtil from '../../../baseRequest'
import requestUrls from '../../../requestUrl'
import imageUtils from '../../../utils/imageUtils.js'
import Notify from '../../../components/vant/notify/notify'
import Dialog from '../../../components/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    complaintList: [],
    imageList: [],
    showLoading: false,
    complaintType: '',
  },
  saveComplaint(form) {
    if (!form.detail.value.complaintReason) {
      this.showMassage("文字描述不能为空");
      return false;
    }
    if(!this.data.orderId){
      //校验数据是否填写
      if (!this.data.complaintType) {
        this.showMassage("申诉类型不能为空");
        return false;
      }
      let formData = {
        openId: wx.getStorageSync('openid'),
        criteria: {
          complaintType: this.data.complaintType,
          complaintReason: form.detail.value.complaintReason,
          imageList: this.data.imageList
        }
      };
      requestUtil.postRequest(requestUrls.complaint.saveOrder, formData, (res) => {
        if (!res.hasError) {
          Dialog.alert({
            message: '提交申诉单成功',
          }).then(() => {
            wx.switchTab({
              url: '/pages/home/index2',
            })
          });
        } else {
          this.showMassage(res.message);
        }
      })
    } 
    //orderId不为空，则为回复申诉单
    if(this.data.orderId){
      let formData = {
        openId: wx.getStorageSync('openid'),
        criteria: {
          mainId:this.data.orderId,
          complaintReason: form.detail.value.complaintReason,
          imageList: this.data.imageList
        }
      };
      requestUtil.postRequest(requestUrls.complaint.saveOrder, formData, (res) => {
        if (!res.hasError) {
          wx.navigateTo({
            url: '/subpackage/pages/complaint/detail?orderId='+this.data.orderId,
          })
        } else {
          this.showMassage(res.message);
        }
      })
    }
    
    
  },
  showMassage(message) {
    Notify({
      type: 'warning',
      message: message
    })
  },
  bindPickerChange(event) {
    let index = event.detail.value;
    this.setData({
      complaintType: this.data.complaintList[index].label,
    })
  },
  afterImageRead(event) {
    const {file} = event.detail;
    let uploadUrl = requestUrls.baseUrl + requestUrls.orderTruckImage.uploadImage+"/COMPLAINT";
    if(file.length>0){
      this.setData({showLoading: true});
      file.forEach((item,index)=>{
        let filePath = item.path;
        imageUtils.uploadImage(uploadUrl,filePath,(url)=>{
          this.setData({
            imageList:[...this.data.imageList,{url: requestUrls.baseImagePrefixUrl + url, imageUrl: url,imageTypeId:'COMPLAINT'}],
            showLoading:!index===file.length
          })
        });
      })
    }
  },
  removeSFZ(event) {
    console.log(event.detail.index);
    let imgList = this.data.imageList;
    imgList.splice(event.detail.index, 1);
    this.setData({
      imageList: imgList,
    });
  },
  getComplaintList() {
    let formData = {
      openId: wx.getStorageSync('openid'),
    };
    requestUtil.postRequest(requestUrls.dictionary.getAllDictionary, formData, (res) => {
      if (res && res.length > 0) {
        res.forEach(item => {
          if (item.value === 'COMPLAINT_TYPE') {
            this.setData({
              complaintList: item.children
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.orderId){
      wx.setNavigationBarTitle({
        title: '回复',
      })
      this.setData({
        orderId:options.orderId
      })
    }
    this.getComplaintList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})